package org.HisCanvas.ChrisRiffle.JFlightLog.swingUi.table;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.HisCanvas.ChrisRiffle.JFlightLog.common.LogBookRecUtils;
import org.HisCanvas.ChrisRiffle.JFlightLog.dataModel.LogBook;
import org.HisCanvas.ChrisRiffle.JFlightLog.dataModel.LogBookRec;




/**
 * FlightLogTableModel
 * 
 * @author RIF
 */
public class FlightLogTableModel extends AbstractTableModel{

		
	
	private List<String> orderedColumnIdList;
	
	private List<LogBookRec> recList;
	
	
	/**
	 * Gets the rec at the specified index of the internal storage collection 
	 * which should coincide w/the rec on the matching index of the row position 
	 * showed on the gui on the screen
	 * 
	 * @param pRowIndex
	 * 			index of record to return
	 * @return
	 * 		rec matching specifed index
	 */
	public LogBookRec getLogBookRec(int pRowIndex){
		
		return recList == null ? null : recList.get(pRowIndex);
	}
	
	
	/**
	 * Constructor
	 * 
	 * @param pLogBook
	 * @param pOrderedColumnIdList
	 */
	public FlightLogTableModel(List<LogBookRec> pRecList, List<String> pOrderedColumnIdList) {
	
		recList = pRecList;
		
		orderedColumnIdList = pOrderedColumnIdList;		
	}
	
	
	
	@Override
	public int getRowCount() {

		return recList == null ? 0 : recList.size();
	}

	
	@Override
	public int getColumnCount() {

		return orderedColumnIdList.size();
	}

	
	
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		
		String columnId = orderedColumnIdList.get(columnIndex);
		
		LogBookRec logBookRec = recList.get(rowIndex); 		
		
		return LogBookRecUtils.getLogBookRecElement(logBookRec, columnId);
		
	}
	
	
	@Override
	public String getColumnName(int pColumnIndex) {
		
		String longFieldName = orderedColumnIdList.get(pColumnIndex);
		
		String shortFieldName = LogBookRecUtils.getFieldShortName(longFieldName);
				
		return shortFieldName;
	}
	
	
	public void refreshTableView(List<LogBookRec> pLogBookRecList){
		
		recList = pLogBookRecList;
		
		fireTableDataChanged();
	}
	
	
	

}
